package com.company.cloud.product.modules.app.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.product.modules.app.service.ProductStockQueryService;
import com.company.cloud.product.modules.base.entity.ProductStockInfo;
import com.company.cloud.product.modules.base.service.ProductStockInfoService;

@Service
public class ProductStockQueryServiceImpl implements ProductStockQueryService {

	@Autowired
	private ProductStockInfoService productStockInfoService;

	@Override
	public Map<String, ProductStockInfo> getPrdStockByPrd(Set<String> productNos) {
		Map<String, ProductStockInfo> stockMap = new HashMap<String, ProductStockInfo>();
		if (!CollectionUtils.isEmpty(productNos)) {
			List<ProductStockInfo> stockList = productStockInfoService.list(new QueryWrapper<ProductStockInfo>()
					.select("product_no AS productNo", "sum(num) AS num", "sum(freeze_num) AS freezeNum")
					.eq("rows_state", EnabledEnum.ENABLED.getCode()).in("product_no", productNos)
					.groupBy("product_no"));
			if (!CollectionUtils.isEmpty(stockList)) {
				stockList.forEach(stock -> {
					stockMap.put(stock.getProductNo(), stock);
				});
			}
		}
		return stockMap;
	}
	
	/**根据商品SKU获取商品SKU库存*/
	@Override
	public Map<String, ProductStockInfo> getSkuStockBySkuNo(Set<String> skuNos){
		Map<String, ProductStockInfo> stockMap = new HashMap<String, ProductStockInfo>();
		if (!CollectionUtils.isEmpty(skuNos)) {
			List<ProductStockInfo> stockList = productStockInfoService.list(new QueryWrapper<ProductStockInfo>()
					.select("sku_no AS skuNo", "sum(num) AS num", "sum(freeze_num) AS freezeNum")
					.eq("rows_state", EnabledEnum.ENABLED.getCode()).in("sku_no", skuNos)
					.groupBy("sku_no"));
			if (!CollectionUtils.isEmpty(stockList)) {
				stockList.forEach(stock -> {
					stockMap.put(stock.getSkuNo(), stock);
				});
			}
		}
		return stockMap;
	}

	@Override
	public Map<String, ProductStockInfo> getSkuStockByPrdNo(String productNo) {
		Map<String, ProductStockInfo> stockMap = new HashMap<String, ProductStockInfo>();
		if(StringUtils.isEmpty(productNo)) {
			return stockMap;
		}
		List<ProductStockInfo> stockList = productStockInfoService
				.list(new QueryWrapper<ProductStockInfo>().eq("product_no", productNo));
		if (!CollectionUtils.isEmpty(stockList)) {
			stockList.forEach(stock -> {
				stockMap.put(stock.getSkuNo(), stock);
			});
		}
		return stockMap;
	}
	
	


}
